//
//  Detail2ViewController.m
//  ManStoreDemo
//
//  Created by qianfeng01 on 15/7/19.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "Detail2ViewController.h"
#import "DetaiModel.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "storeViewController.h"
#import "DBManager.h"

#define kScreenSize [UIScreen mainScreen].bounds.size
#define kUrl @"http://api.nanyibang.com/items?age=21&height=170&item_id=%ld&skin_type=1&style=1&weight=60"

@interface Detail2ViewController ()
{
    AFHTTPRequestOperationManager *_manager;
}
@property (nonatomic,strong) DetaiModel *detailModel;
@end

@implementation Detail2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self ifExist];
    [self getLine];
    [self loadData];
    [self setbutton];
    [self createFooterView];
}

-(void)ifExist{
    BOOL isExist=[[DBManager sharedManager] isExistForitem_id:self.item_id];
    [self.favoritebutton setTitle:@"已收藏" forState:UIControlStateDisabled];
    [self.favoritebutton setTitleColor:[UIColor yellowColor] forState:UIControlStateDisabled];
    if (isExist) {
        self.favoritebutton.enabled=NO;
    }else{
        self.favoritebutton.enabled=YES;
    }
}

-(void)getLine{
    CGRect frame=self.priceLabel.frame;
    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(-5, frame.size.height/2, frame.size.width-23, 1)];
    lineView.backgroundColor=[UIColor redColor];
    [self.priceLabel addSubview:lineView];
}
-(void)setbutton{
    UIBarButtonItem *leftitem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return_1"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    self.navigationItem.leftBarButtonItem=leftitem;
}
-(void)back:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loadData{
    NSString *url=[NSString stringWithFormat:kUrl,(long)self.item_id.integerValue];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            self.detailModel=[[DetaiModel alloc] init];
            self.detailModel.pic_url=dict[@"data"][@"pic_url"];
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.pic_url]];
            self.detailModel.title=dict[@"data"][@"title"];
            self.detailModel.coupon_price=dict[@"data"][@"coupon_price"];
            self.coupon_priceLabel.text=[NSString stringWithFormat:@"￥%@",self.detailModel.coupon_price];
            self.detailModel.num_iid=dict[@"data"][@"num_iid"];
            self.detailModel.item_id=dict[@"data"][@"item_id"];
            
            self.detailModel.zhekou=dict[@"data"][@"zhekou"];
            
            self.titleLabel.text=self.detailModel.title;
            self.priceLabel.text=dict[@"data"][@"price"];
            self.zhekouLabel.text=self.detailModel.zhekou;
            self.nameLabel.text=dict[@"data"][@"brand"];
            self.materialLabel.text=dict[@"data"][@"material"];
            self.styleLabel.text=dict[@"data"][@"style"];
            self.numberLabel.text=self.detailModel.num_iid;
            self.colorLabel.text=dict[@"data"][@"color"];
            self.seasoLabel.text=dict[@"data"][@"season"];
            if (self.detailModel.zhekou==nil) {
                self.TuPianImange.hidden=YES;
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}
-(void)createFooterView{
    UIView *footerView=[[UIView alloc] initWithFrame:CGRectMake(0, kScreenSize.height-40, kScreenSize.width, 40)];
    footerView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:footerView];

}

-(void) viewDidAppear:(BOOL)animated{
    
    [self.scrollView setContentSize:CGSizeMake(kScreenSize.width,730)];
    
}
- (IBAction)Buy:(id)sender {
    storeViewController *controller=[[storeViewController alloc] init];
    controller._id= self.numberLabel.text;
    [self.navigationController pushViewController:controller animated:YES];
}
- (IBAction)favoriteClick:(id)sender {
    UIButton *button = sender;
    //禁用
    button.enabled = NO;
    //收藏 记录到本地数据库
    [[DBManager sharedManager] insertModel:self.detailModel];
}

@end
